package com.card.framework.security;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.util.TreeBulid;
import com.card.manager.dao.RolesMapper;
import com.card.manager.dao.UsersMapper;
import com.card.manager.domain.Roles;
import com.card.manager.domain.Users;
import com.card.manager.domain.UsersExample;

/**
 * @description  项目实现的用户查询服务,将用户信息查询出来(用于实现用户的认证)
 * @author aokunsang
 * @date 2012-8-15
 */
public class MyUserDetailServiceImpl extends MyFrameworkSupperMapper implements UserDetailsService {
	@Autowired(required=true) 
	private TreeBulid treeBulid;
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		
		System.out.println("---------MyUserDetailServiceImpl:loadUserByUsername------正在加载用户名和密码，用户名为："+username);
		//这可以不用查询
		UsersMapper userMapper = this.getMapper(UsersMapper.class);
		UsersExample userExample = new UsersExample();
		userExample.createCriteria().andUsernameEqualTo(username);
		Users users = userMapper.selectByExample(userExample).get(0);
		if(users==null){
			throw new UsernameNotFoundException("用户名没有找到!");
		}
//		
		boolean enabled = true;                //是否可用
        boolean accountNonExpired = true;        //是否过期
        boolean credentialsNonExpired = true;   
        boolean accountNonLocked = true;  
        
		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();

		RolesMapper rolesMapper = this.getMapper(RolesMapper.class);
		
		List<Roles> roles = rolesMapper.selectRolesForUser(users.getUserid());
		String roleName="";
		for(int i = 0 ; i <roles.size() ; i++){
			Roles role = roles.get(i);
			if(i==0) {
				roleName = role.getDescs();
			}else{
				roleName = roleName + " , " + role.getDescs();
			}
			GrantedAuthority ga = new SimpleGrantedAuthority(role.getName());
			authorities.add(ga);	
		}
//		SecurityUser user = new SecurityUser();
//		user.setUserId(user.getUserId());
//		user.setPassword(users.getPassword());
//		user.setUsername(users.getUsername());
//		user.setAccountNonExpired(accountNonExpired);
//		user.setCredentialsNonExpired(credentialsNonExpired);
//		user.setAccountNonLocked(accountNonLocked);
//		user.setMenu(treeBulid.buildAllTree(1,2));
		return new SecurityUser(
				users.getUsername(),
				users.getPassword(), 
				enabled, 
				accountNonExpired, 
				credentialsNonExpired, 
				accountNonLocked, 
				authorities,
				treeBulid.buildAllTree(users.getUserid(),1,-1),roleName);
	}

}
